package com.quantdo.demo.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends TitleAreaDialog {

	private Text userName;
	private Text password;
	
	public LoginDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("User Login");
		newShell.setSize(500,230);
		newShell.setLocation(260, 260);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control control = super.createContents(parent);
		this.setTitle("Add login info");
		this.setMessage("Is not null", IMessageProvider.INFORMATION);
		return control;
	}

	
	@Override
	protected int getShellStyle() {
		return super.getShellStyle()|SWT.RESIZE|SWT.MAX|SWT.MIN;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		System.out.printf("class:%s, function:%s() enter \n", LoginDialog.class, "createDialogArea");
		Composite composite = new Composite(parent, SWT.BORDER);
		composite.setLayout(new GridLayout(2, false));
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		composite.setLayoutData(gridData);
		
		new Label(composite,SWT.NONE).setText("User Name:");
		userName = new Text(composite, SWT.BORDER);
		userName.setLayoutData(gridData);
		new Label(composite,SWT.NONE).setText("Password:");
		password = new Text(composite, SWT.BORDER);
		password.setEchoChar('*');
		password.setLayoutData(gridData);
		
		return composite;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if(IDialogConstants.OK_ID == buttonId){
			if(!checkValue()) return;
			getValue();
		}
		okPressed();
		if(IDialogConstants.CANCEL_ID == buttonId){
			cancelPressed();
		}
		super.buttonPressed(buttonId);
	}

	private boolean checkValue(){
		System.out.printf("class:%s, function:%s() enter \n", LoginDialog.class, "checkValue");
		String un = userName.getText();
		String pw = password.getText();
		System.out.printf("user:%s, password:%s\n", un, pw);
		if(null == un || null == pw) return false;
		return true;
	}
	
	private void getValue(){
		System.out.printf("class:%s, function:%s() enter \n", LoginDialog.class, "getValue");
	}
}
